#!/bin/bash
#
# @file check_package
# @brief check script for OpenRTP eclipse packages
# @author Noriaki Ando <n-ando@aist.go.jp>
#
#------------------------------
#
#

true ${WORK_DIR:=work_$$}
# target projects
PROJECTS="jp.go.aist.rtm.toolscommon.profiles
    jp.go.aist.rtm.toolscommon.profiles.nl1
    jp.go.aist.rtm.toolscommon
    jp.go.aist.rtm.toolscommon.nl1
    jp.go.aist.rtm.rtcbuilder
    jp.go.aist.rtm.rtcbuilder.nl1
    jp.go.aist.rtm.rtcbuilder.java
    jp.go.aist.rtm.rtcbuilder.python
    jp.go.aist.rtm.rtcbuilder.lua
    jp.go.aist.rtm.repositoryView
    jp.go.aist.rtm.repositoryView.nl1
    jp.go.aist.rtm.nameserviceview
    jp.go.aist.rtm.nameserviceview.nl1
    jp.go.aist.rtm.systemeditor
    jp.go.aist.rtm.systemeditor.nl1"

#------------------------------------------------------------
# get_version
#
# This function gets version number and project version string
# from version text file.
# ------------------------------------------------------------
get_version()
{
    if test "x$VERSION" = "x" || test "x$PROJECT_VERSION" = "x" ; then
        echo "Environment variable VERSION/PROJECT_VERSION is not set."
        echo "Getting from ./version text."
        if test ! -f version.txt ; then
            echo "version.txt not found. Plugins should be built at first."
            echo "Aborting."
            exit 1
        fi
        . ./version.txt
    fi
    export VERSION
    export PROJECT_VERSION

    if test "x$DISTDIR" = "x" ; then
        DISTDIR=openrtp-$VERSION
    fi
    if test "x$JARDIR" = "x" ; then
        JARDIR=$JARDIR_DEFAULT
    fi
}


#------------------------------------------------------------
# check_archive
#------------------------------------------------------------
check_archive()
{
    rm -rf $WORK_DIR/eclipse
    mkdir -p $WORK_DIR
    zip=`echo $FILE_NAME | grep '[zZ][iI][pP]'`
    tar=`echo $FILE_NAME | grep '[tT][aA][rR].[gG][zZ]'`
    if test ! "x$zip" = "x" ; then
        echo "Extracting zip file: $FILE_NAME ..."
        unzip $FILE_NAME -d $WORK_DIR >/dev/null 2>&1 && return 0
        echo "Failed to extract zip file: $FILE_NAME. Aborting..."
        return 1
    elif test ! "x$tar" = "x" ; then
        echo "Extracting tar.gz file: $FILE_NAME ..."
        tar xvzf $FILE_NAME -C $WORK_DIR >/dev/null 2>&1 && return 0
        echo "Failed to extract tar.gz file: $FILE_NAME. Aborting..."
        return 1
    else
        echo "Unknown file extention. Aborting..."
        return 1
    fi
    return 1
}

check_dependent_plugins()
{
    plugins="org.eclipse.emf_*.jar
org.eclipse.gef_*.jar
org.eclipse.xsd_*.jar
org.eclipse.emf.ecore.xcore_*.jar"
    for p in $plugins ; do
        tmp=`ls $WORK_DIR/eclipse/plugins/$p`
        if test "x$tmp" = "x" ; then
            echo "[ERROR] Plugin: $p does not exist. Aboring."
            return 1
        else
            file_name=`basename $tmp`
            echo "[OK]    Plugin: $file_name exists."
        fi
    done
    return 0
}

check_dependent_features()
{
    features="org.eclipse.emf_*
              org.eclipse.emf.ecore.xcore_*
              org.eclipse.xsd_*
              org.eclipse.gef_*"
    for p in $features ; do
        tmp=`ls -d $WORK_DIR/eclipse/features/$p`
        if test "x$tmp" = "x" ; then
            echo "[ERROR] Feature: $p does not exist. Aboring."
            return 1
        else
            file_name=`basename $tmp`
            echo "[OK]    Feature: $file_name exists."
        fi
    done
    return 0
}

check_openrtp_plugins()
{
    is_ja=`echo $FILE_NAME | grep 'ja'`
    for p in $PROJECTS ; do
        jar_name="${p}_${PROJECT_VERSION}.jar"
        is_nl=`echo $p | grep '.nl'`
        if test "x$is_nl" != "x" -a "x$is_ja" = "x" ; then
            continue
        fi
        if test ! -f $WORK_DIR/eclipse/plugins/$jar_name ; then
            echo "[ERROR] Plugin file $jar_name does not exist."
            return 1
        else
            echo "[OK]    Plugin: $jar_name exists."
        fi
    done
    return 0
}

check_openrtp_features()
{
    is_ja=`echo $FILE_NAME | grep 'ja'`
    if test "x$is_ja" = "x" ; then
        features="jp.go.aist.rtm.rtcbuilder.feature_*
                  jp.go.aist.rtm.rtsystemeditor.feature_*"
    else
        features="jp.go.aist.rtm.rtcbuilder.feature_*
                  jp.go.aist.rtm.rtcbuilder.nl1.feature_*
                  jp.go.aist.rtm.rtsystemeditor.feature_*
                  jp.go.aist.rtm.rtsystemeditor.nl1.feature_*"
    fi
    for p in $features ; do
        tmp=`ls -d $WORK_DIR/eclipse/features/$p`
        if test "x$tmp" = "x" ; then
            echo "[ERROR] Feature: $p does not exist."
            return 1
        else
            file_name=`basename $tmp`
            echo "[OK]    Feature: $file_name exists."
        fi
    done
    return 0
}

check_langpack()
{
    is_ja=`echo $FILE_NAME | grep 'ja'`
    if test "x$is_ja" = "x" ; then
        # this is not Japanese eclipse package
        return 0
    fi

    # checking pleiades
    if test -d $WORK_DIR/eclipse/plugins/jp.sourceforge.mergedoc.pleiades; then
        echo "[OK]    Pleiades installed."
        return 0
    fi
    # checking langpack
    tmp=`ls $WORK_DIR/eclipse/plugins/*nl_ja*`
    if test "x$tmp" != "x" ; then
        echo "[OK]    Lang pack installed."
        return 0
    fi

    echo "[ERROR] No language package installed."
    return 1
}

check_package()
{
    FILE_NAME=$1
    if check_archive && \
		check_dependent_plugins && \
		check_dependent_features && \
		check_openrtp_plugins && \
		check_openrtp_features && \
		check_langpack ; then
		OK_FILES="$OK_FILES $FILE_NAME"
	else
		BROKEN_FILES="$BROKEN_FILES $FILE_NAME"
	fi
}

check_packages()
{
    for f in $* ; do
        check_package $f
    done

	echo ""
	echo "------------------------------------------------------------"
	echo " Correct Package"
	echo "------------------------------------------------------------"
	for f in $OK_FILES ; do
		echo $f
	done
	
	echo ""
	echo "------------------------------------------------------------"
	echo " Broken Packages"
	echo "------------------------------------------------------------"
	for f in $BROKEN_FILES ; do
		echo $f
	done
	
}


#------------------------------
# main
#------------------------------
if test $# -eq 0 ; then
    echo "Usage: $0 <OpenRTP eclipse packages>"
    echo ""
    echo "Example:"
    echo "  $ $0 packages/eclipse381*"
    echo ""
    exit 0
fi

get_version
check_packages $*
rm -rf $WORK_DIR

if test "x$BROKEN_FILES" != "x" ; then
	exit 1
fi
exit 0
